package com.kun.index.controller;


import com.kun.common.Constants;
import com.kun.common.ResultDto;
import com.kun.index.controller.dto.LoginDto;
import com.kun.index.entity.User;
import com.kun.index.service.impl.UserServiceImpl;
import com.kun.utils.TokenUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-02-27
 */
@RestController
@RequestMapping("/index/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @PostMapping("/login")
    @ApiOperation("登录后台界面")
    public ResultDto login(@RequestBody LoginDto user){
        return userService.login(user);

    }

    @PostMapping("/saveUserInfo")
    @ApiOperation("保存用户信息")
    public ResultDto saveUserInfo(@RequestBody User user){

        if (userService.saveUser(user)>0){
            User currentUser = userService.queryUserById(user.getId());
            return ResultDto.success(currentUser);
        }else{
            return ResultDto.fail(Constants.CODE_302,"用户不存在");
        }
    }

    @GetMapping("/saveUserPassword")
    @ApiOperation("修改密码")
    public ResultDto saveUserPassword(@RequestParam("id") Integer id,
                                      @RequestParam("oldPassword") String oldPassword,
                                      @RequestParam("newPassword") String newPassword){
        if (userService.updatePassword(id,oldPassword,newPassword)){
            return ResultDto.success(TokenUtils.generateToken(""+id,newPassword));
        }else {
            return ResultDto.fail("原密码错误");
        }

    }


}

